[% setvar title The AUTOLOAD subroutine should be able to decline a request %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>The AUTOLOAD subroutine should be able to decline a request</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Leon Brocard &lt;<a href='mailto:acme@astray.com'>acme@astray.com</a>&gt;
  Date: 10 Aug 2000
  Last Modified: 25 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 8
  Version: 3
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>In Perl 5, the first AUTOLOAD subroutine found in an object's hierarchy
is used and any other AUTOLOAD subroutines higher up are ignored. I
propose that in Perl 6 we allow AUTOLOAD subroutines to decline a
request, which allows other AUTOLOADs to cope with it.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>AUTOLOAD is a very useful and handy way to handle cool and exciting
things, but as has been pointed out many times before, only one
AUTOLOAD can be called in the object's hierarchy, even if multiple ones
exist. This is due to the fact that AUTOLOAD subroutines must do
something - they can not currently decline a request. I propose one of
the methods which has previously been discussed on p5p.</p>
<p>Instead of calling the right thing or actually doing the right thing,
AUTOLOAD subroutines should return a coderef which will be run as if
it were the method called. If an AUTOLOAD subroutine does not wish to
cope with a method call, it should return undef. Perl would then walk
the OO hierarchy and find the next AUTOLOAD to call, eventually failing
with an error if no AUTOLOAD method is found which will accept the
call.</p>
<p>Some other RFC's have appeared which mention alternative ways of
coping with this problem. Some of them are cute, but there has not
really been much discussion of the methods. See the references section
for more.</p>
<a name='EXAMPLE'></a><h2>EXAMPLE</h2>
<p>The following Perl 5 code (an extension of the AUTOLOAD example in the
perlsub manpage):</p>
<pre>  sub AUTOLOAD {
    my $program = $AUTOLOAD;
    my @args = @_;
    $program =~ s/.*:://;
    if ($program =~ /^[aeiou]/) {
      system($program, @args);
    }
  }</pre>
<p>... would be rendered as the following in Perl 6:</p>
<pre>  sub AUTOLOAD {
    my $program = $AUTOLOAD;
    my @args = @_;
    $program =~ s/.*:://;
    if ($program =~ /^[aeiou]/) {
      return sub { system($program, @args) }
    } else {
      return undef;
    }
  }</pre>
<a name='$AUTOLOAD'></a><h2>$AUTOLOAD</h2>
<p>While we're at it, it *may* be a good idea to remove the global
$AUTOLOAD variable and instead pass it as the first parameter of the
AUTOLOAD subroutine call. For: general global drought, the fact that
perlsub's argument &quot;because, er, well, just because, that's why...&quot; is
a bit weak. Against: makes AUTOLOAD more complicated, breaking the
&quot;subroutine parameters end up as @_&quot; paradigm (apparently).</p>
<a name='UNIVERSAL-&gt;can'></a><h2>UNIVERSAL-&gt;can</h2>
<p>This proposal has the added bonus that the UNIVERSAL-&gt;can method (or
whatever replaces it) will now work with AUTOLOAD-ed methods, whereas
in Perl 5 it used to fail.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>This strikes me as being a fairly easy thing to do, but then again
internals ain't my thing, baby.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Mike Guy's &quot;AUTOLOADREF&quot; idea on p5p:
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-03/msg01317.html' target='_blank'>www.xray.mpe.mpg.de</a></p>
<p>Ilya Zakharevich &quot;sub autoload&quot; idea on p5p:
<a href='http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-01/msg01381.html' target='_blank'>www.xray.mpe.mpg.de</a></p>
<p>RFC 190: &quot;Objects : NEXT pseudoclass for method redispatch&quot; by Damian Conway
&lt;<a href='mailto:damian@conway.org'>damian@conway.org</a>&gt;</p>
<p>RFC 232: &quot;Replace AUTOLOAD by a more flexible mechanism&quot; by Ilya Zakharevich</p>
</div>
